import tensorflow as tf
import captcha_1 as cx
import numpy as np
import random as rd
#from tensorflow.contrib.learn.python.learn.datasets import base
base_dir = "D:/box/captcha1/"

# 用tensorflow 实现 最简单的验证码识别 99.87 的识别率

def next_batch():
    x1,y1 = cx.getData()

    #start = int(rd.random()*3900)
    #print("start index:",start)
    #end = 100 + start
    #x1 = x1[start:end]
    #y1 = y1[start:end]


    return np.asarray(x1),np.asarray(y1)

def next_test_batch():
    return cx.getTestData()

test_data = tf.Variable(tf.zeros([50,20*13]))
x = tf.placeholder(dtype=tf.float32,shape=[None,20*13])
y = tf.placeholder(dtype=tf.float32,shape=[None,10])

w1 = tf.Variable(tf.zeros(shape=[20*13,10]))
b1 = tf.Variable(tf.zeros(shape=[1,10]))

actv = tf.nn.softmax(tf.matmul(x,w1)+b1)
loss = -tf.reduce_sum(y*tf.log(actv))
train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)

    for i in range(100):
        xs,ys = next_batch()
        sess.run(train_step,feed_dict={x:xs,y:ys})

        xts,yts = next_test_batch()
        predication = tf.equal(tf.argmax(actv,1),tf.argmax(y,1))
        acc = tf.reduce_mean(tf.cast(predication,dtype=tf.float32))
        print("acc:",sess.run(acc,feed_dict={x:xs,y:ys}))